/*
 * @Author: spark
 * @Date: 2021-06-21 14:11:52
 * @LastEditTime: 2021-06-28 17:41:54
 * @LastEditors: spark
 * @Description: 一个持续与bug做抗争的程序员
 * email: spark.xiaoyu@qq.com
 */
// import { MutationTree } from '@/store'
import { setToken, removeToken, getToken } from '@/utils/cookies'
import type { UserState } from './state'
import { UserMutationsTypes } from './mutations-types'
import { resetRouter } from '@/router'


export type Mutations<S = UserState> = {
    [UserMutationsTypes.MUTATIONS_SET_TOKEN](state: S, token: string): void
    [UserMutationsTypes.MUTATIONS_REMOVE_TOKEN](state: S): void
}

export const mutations: Mutations = {
    [UserMutationsTypes.MUTATIONS_SET_TOKEN](state, token) {
        state.token = token
        setToken(token)
    },
    [UserMutationsTypes.MUTATIONS_REMOVE_TOKEN](state) {
        removeToken()
        state.token = getToken()
        resetRouter()
    }
}